package com.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.mywork.R;

import java.util.Calendar;


/**
 * Created by wayne on 2015/7/12.
 */
public class DataTimePicker
        extends
        AlertDialog
        implements
        DialogInterface.OnClickListener,
        DatePicker.OnDateChangedListener
{
	
	private DatePicker            datePicker;
	private TimePicker            timePicker;
	private Context               context;
	private OnDateTimeSetListener mDateSetListener;
	
	public interface OnDateTimeSetListener
	{
		void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth, int hour, int mintue);
	}
	
	public DataTimePicker(Context context, OnDateTimeSetListener dateSetListener) {
		super(context);
		this.context = context;
		this.mDateSetListener = dateSetListener;
		
		findViews();
		
		setDateTime();
		getWindow().setLayout(ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.BOTTOM);
		getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
	}
	
	void findViews() {
		View view = LayoutInflater.from(context).inflate(R.layout.pick_date_time, null);
		datePicker = (DatePicker) view.findViewById(R.id.dpPicker);
		timePicker = (TimePicker) view.findViewById(R.id.tpPicker);
		setView(view);
		
		setButton(BUTTON_POSITIVE, context.getString(R.string.pick_ok), this);
		setButton(BUTTON_NEGATIVE, context.getString(R.string.pick_cancel), this);
	}
	
	void setDateTime() {
		Calendar c = Calendar.getInstance();
		int mMinYear = c.get(Calendar.YEAR);
		int mMinMonth = c.get(Calendar.MONTH);
		int mMinDay = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		
		datePicker.init(mMinYear, mMinMonth, mMinDay, new DatePicker.OnDateChangedListener()
		{
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				// 获取一个日历对象，并初始化为当前选中的时间
				Calendar calendar = Calendar.getInstance();
				
				calendar.set(year, monthOfYear, dayOfMonth);
			}
		});
		
		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(hour);
		timePicker.setCurrentMinute(minute);
		timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
		{
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				Calendar calendar = Calendar.getInstance();
//				calendar.set(hourOfDay, minute);
			}
		});
	}
	
	@Override
	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
			case BUTTON_POSITIVE:
				if (mDateSetListener != null)
				{
					mDateSetListener.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), timePicker.getCurrentHour(), timePicker.getCurrentMinute());
				}
				break;
			case BUTTON_NEGATIVE:
				cancel();
				break;
		}
	}
	
	@Override
	public void onDateChanged(DatePicker view, int year, int month, int day) {
		datePicker.init(year, month, day, this);
	}

	@Override
	public void show() {
		super.show();
		Button positiveButton = getButton(BUTTON_POSITIVE);
		Button negativeButton = getButton(BUTTON_NEGATIVE);
		positiveButton.setTextColor(context.getResources().getColor(android.R.color.holo_blue_light));
		negativeButton.setTextColor(context.getResources().getColor(android.R.color.holo_blue_light));
	}
}
